import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './user.entity';
import { Logs } from '../logs/logs.entity';

@Injectable()
export class UserService {
    constructor(
        @InjectRepository(User)
        private userRepository: Repository<User>,
        @InjectRepository(Logs)
        private logsRepository: Repository<Logs>,
    ) {}

    findAll() {
        return this.userRepository.find();
    }

    find(username: string){
        return this.userRepository.findOne({ where: { username } });
    }

    findOne(id: number){
        return this.userRepository.findOne({ where: { id } });
    }

    async create(user: User) {
        const userTmp = await this.userRepository.create(user);
        return this.userRepository.save(userTmp);
    }

    async update(id: number, user: Partial<User>) {
        return this.userRepository.update(id, user);
    }

    remove(id: number) {
        return this.userRepository.delete(id);
    }

    findProfile(id: number){
        return this.userRepository.findOne({
            where: { id },
            relations: {
                profile: true
            }
        })
    }

    async findUserLogs(id: number){
        const user = await this.findOne(id);
        return this.logsRepository.find({
            where: { user},
            relations: {
                user: true
            }
        })
    }

    findLogsByGroup(id: number){
        return this.logsRepository
            .createQueryBuilder('logs')
            .select('logs.result', 'result')
            .addSelect('COUNT("logs.result")', 'count')
            .leftJoinAndSelect('logs.user', 'user')
            .where('user.id = :id', { id })
            .groupBy('logs.result')
            .getRawMany();
    }
}
